
/**
 * 客档选择插件
 * 
 */
;(function($){
	//入口
	$.fn.selection_fromProfile=function(options){
		var el = this;
		//默认参数
		var init = {
			search : "",//模糊查询数据
			pno : "",//客人ID
			pnoname : "pno",
			condition:{"ptype":1},//获取数据源条件json
			gname : "",
			tel : "",
			contextPath:$.HSAPI_AJAXURL,
			oper : "s",//o:进行数据保存, s:返回选择档案数据;
			data:{
				code : "gname", //需要获取字段名
				container : "gname", //填入input的ID
				multiSelect : "no", //是否多选
			},
			dataBack : function(){},//回调函数
			closeBack : function(){},//回调函数
			searchValueBack:function(){},//返回查询数据
			searchBack:function(){}//点击查询返回按钮
		}
		var param = $.extend(init,options);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.initGrid();
		this.bindEvent();
	}
	
	App.prototype = {
			
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//搜索框回车事件
			this.el.find("#filterSearch").keydown(function(e){
				if(e.keyCode == 13){
					el.find("#btnFilterSearch").trigger("click");
				}
			})
			
			//查询按钮查询
			this.el.find("#btnFilterSearch").click(function(){
				param.search = el.find("#filterSearch").val();
				el.find("#profileInfo").find("span").each(function(){
		    		$(this).text("");
		    	})
				t.setGridValue();
				param.searchBack(param.search);
			})
			
			el.find("#frm").change(function(){
				el.find("#btnFilterSearch").trigger("click");
			})
			
			el.find("#frm").keyup(function(e){
				if(e.keyCode==13){
					el.find("#btnFilterSearch").trigger("click");
				}
			})
			
			//模态关闭
			this.el.find("#modalBtnCancel,#close-dialog").click(function(){
				el.empty();
				el.modal("hide");
				param.closeBack();
			});
			
			//保存
			this.el.find("#btnSave").click(function(){
				var pnonameList = {
					pno : 0,
					acc_pno1 : 1,
					acc_pno2 : 2,
					acc_pno3 : 3,
					acc_pno4 : 4,
				}
				var pnoname = param.pnoname;
				var rowid = el.find("#gridTableFromProfile").jqGrid("getGridParam","selrow");
				var rowdata = el.find("#gridTableFromProfile").jqGrid("getRowData",rowid);
				rowdata = $.parseJSON(JSON.stringify(rowdata).replace(/<[^>]+>/g,""));
				var num = pnonameList[pnoname];
				if(rowid){
					var profilepno = "profilepno_"+num;
					if(param.oper=="o"){
						var str = t.saveProfile(num,rowdata,pnoname);
						if(str==1){
							param.dataBack();
							t.activeProfile(rowdata.pno);
							el.empty();
							el.modal("hide");
						}
					}else if(param.oper=="s"){
						var json = {profilepno:profilepno}
						json = $.extend(json,rowdata)
						param.dataBack(json);
						t.activeProfile(rowdata.pno);//激活客人档案
						el.empty();
						el.modal("hide");
					}
				}else{
					jf_alert($.i18n.propJsp("请选择一条数据"))
				}
			});
		},
		initHtml : function(){
			//创建模态窗体
			this.el.addClass("modal").attr("tabindex","-1").attr("data-backdrop","static");
			var html=	'<div class="modal-dialog" style="min-width: 1000px;">'+
			    			'<div class="modal-content">'+
					  			'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="modal-h4">'+$.i18n.propJsp("客人档案选择")+'</span>'+
									'</div>'+
								'</div>'+
					  			'<div class="modal-body">'+
									'<div class="row">'+
										'<div class="col-xs-8">'+
											'<div class="well well-sm">'+
												'<form id="frm" class="form-horizontal" action="javascript:void(0);">'+
													'<div class="form-group">'+
														'<label class="col-sm-2 control-label">'+$.i18n.propJsp("姓名")+'</label>'+
														'<div class="col-sm-4">'+
															'<input id="gname" name="gname" type="text" class="input-medium" style="height:30px;"/>'+
														'</div>'+
														'<label class="col-sm-2 control-label">'+$.i18n.propJsp("电话")+'</label>'+
														'<div class="col-sm-4">'+
															'<input id="tel" name="tel" type="text" class="input-medium" style="height:30px;"/>'+
														'</div>'+
													'</div>'+
													'<div class="form-group">'+
														'<label class="col-sm-2 control-label">'+$.i18n.propJsp("证件号")+'</label>'+
														'<div class="col-sm-4">'+
															'<input id="passno" name="passno" type="text" class="input-medium" style="height:30px;"/>'+
														'</div>'+
														'<label class="col-sm-2 control-label"></label>'+
														'<div class="col-sm-4">'+
															'<label style="margin-top:5px;">'+
																'<input id="like" name="like" type="checkbox" onchange="this.value=(this.checked==true?1:0)" class="ace" value="1" checked/>'+
																'<span class="lbl">'+$.i18n.propJsp("模糊")+'</sapn>'+
															'</label>'+
															'<span class="btn-group" style="float:right;">'+
																'<button id="btnFilterSearch" class="btn btn-sm btn-info">'+
																	'<i class="ace-icon fa fa-search"></i>'+
																	$.i18n.propJsp("查询")+
																'</button>'+
															'</span>'+
														'</div>'+
													'</div>'+
												'</form>'+
											'</div>'+
											'<div class="widget-box">'+
								  				'<div id="gridWidth" class="widget-body">'+
													'<table id="gridTableFromProfile"></table>'+
													'<div id="grid-pager-checkbox"></div>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="col-xs-4" style="padding-left:0px;">'+
											'<div class="widget-box ">'+
												'<div class="widget-body">'+
													'<div class="widget-main" style="height:440px;margin-top:1px;overflow:auto;">'+
														'<div id="profileInfo" class="profile-user-info">'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("姓名")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="gname"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("证件号")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="passno"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("电话")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="tel"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">PNO</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="pno"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("别名")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="fname"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("地址")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="addr"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("公司")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="company"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("费率连接")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="ratelink"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("销售员")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="sales_name"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("上次房号")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="last_room"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("来源")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="sou"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;">'+$.i18n.propJsp("特征")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="feature"></span>'+
																'</div>'+
															'</div>'+
															'<div class="profile-info-row">'+
																'<div class="profile-info-name" style="width:30%;" style="vertical-align: baseline;">'+$.i18n.propJsp("备注")+'</div>'+
																'<div class="profile-info-value" style="width:70%;">'+
																	'<span id="remark"></span>'+
																'</div>'+
															'</div>'+
														'</div>'+
													'</div>'+
													'<div class="widget-toolbox padding-8 clearfix" style="height:53px;">'+
														'<div class="pull-right" style="margin-top:4px;">'+
															'<div class="btn-group">'+
																'<button class="btn btn-info btn-sm" id="btnSave" data-id="pno">'+
																	'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("保存")+
																'</button>'+
															'</div>'+
															'<div class="btn-group">'+
																'<button id="modalBtnCancel" class="btn btn-default btn-sm" data-dismiss="modal">'+
																	'<i class="ace-icon fa fa-times"></i>'+$.i18n.propJsp("取消")+
																'</button>'+
															'</div>'+
														'</div>'+
													'</div>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
			    			'</div>'+
			    		'</div>';
			this.el.html(html);
			var search = this.param.search;
			this.el.find("#filterSearch").val(search);
			this.el.find("#filterSearch").focus();
//			this.el.modal('show');
			this.el.find("#gname").val(this.param.gname);
			this.el.find("#tel").val(this.param.tel);
			
			this.el.find("#modal-h4").html($.i18n.propJsp("请选择")+'&nbsp;'+'<i class="ace-icon fa fa-angle-double-right"></i>&nbsp;'+$.i18n.propJsp("客人档案"));
		},
		initGrid : function(){//jqgrid初始化
			var grid_selector = this.el.find("#gridTableFromProfile");
			var pager_selector = this.el.find("#grid-pager-checkbox");
			var el = this.el;
			var param=this.param;
			var initc = this.getDs_1();
			
			//构造表格
			grid_selector.jqGrid({
				url : $.HSAPI_AJAXURL+$.HSAPI_PROCEDURE2_LIST,
				datatype : "json",
				width :633,
				height : 300,
				postData : $.HSAPI_ASKJSONINFO(JSON.stringify(initc),'ListRetrieveHoProfilePersonAndBoot',"{}","listWithPage"),
				colNames : ["","PNO","姓名","别名","证件号","电话","地址","公司","性别","vip","特征",
				            "备注","城市","上次房号",
				            "last_conc","费率连接","销售员","级别","国家","档案类型",
				            "档案有效期","contract_date","createdate","cre_date","userid",
				            "来源","旅行社","flags","comp_id"],
				colModel : [{
					name : "isboot",//"isboot",
					index : "isboot",
					width : 40,
					align : "center",
					formatter : function(cellvalue,options,rowObject){
						var str = '';
						str = '<span style="font-size:15px;font-weight:bold;color:#6fb3e0;">'+cellvalue+'</span>';
						return str;
					}
				},{
					name : "pno",//"PNO",
					index : "pno",
					width : 200,
					hidden : true,
				},{
					name : "gname",//"姓名",
					index : "gname",
					width : 100,
				},{
					name : "fname",//"别名",
					index : "fname",
					width: 140,
					hidden : true,
				},{
					name : "passno",//"证件号",
					index : "passno",
					width: 180,
//					hidden : true,
				},{
					name : "tel",//"电话",
					index : "tel",
					width: 120,
//					hidden : true,
				},{
					name : "addr",//"地址",
					index : "addr",
					width: 240,
//					hidden : true,
				},{
					name : "company",//"公司",
					index : "company",
					width: 240,
//					hidden : true,
				},{
					name : "sex",//"性别",
					index : "sex",
					width: 90,
					hidden : true,
				},{
					name : "vip",//"vip",
					index : "vip",
					width: 180,
					hidden : true,
				},{
					name : "feature",//"特征",
					index : "feature",
					width: 240,
					hidden : true,
				},{
					name : "remark",//"备注",
					index : "remark",
					width: 240,
					hidden : true,
				},{
					name : "city",//"城市",
					index : "city",
					width: 240,
					hidden : true,
				},{
					name : "last_room",//"上次房号",
					index : "last_room",
					width: 240,
					hidden : true,
				},{
					name : "last_conc",//"last_conc",
					index : "last_conc",
					width: 240,
					hidden : true,
				},{
					name : "ratelink",//"费率连接",
					index : "ratelink",
					width: 240,
					hidden : true,
				},{
					name : "sales_name",//"销售员",
					index : "sales_name",
					width: 240,
					hidden : true,
				},{
					name : "levels",//"级别",
					index : "levels",
					width: 240,
					hidden : true,
				},{
					name : "country",//"国家",
					index : "country",
					width: 240,
					hidden : true,
				},{
					name : "ptype",//"档案类型",
					index : "ptype",
					width: 240,
					hidden : true,
				},{
					name : "saveguest",//"档案有效期",
					index : "saveguest",
					width: 240,
					hidden : true,
				},{
					name : "contract_date",//"contract_date",
					index : "contract_date",
					width: 240,
					hidden : true,
				},{
					name : "createdate",//"createdate",
					index : "createdate",
					width: 240,
					hidden : true,
				},{
					name : "cre_date",//"cre_date",
					index : "cre_date",
					width: 240,
					hidden : true,
				},{
					name : "userid",//"userid",
					index : "userid",
					width: 240,
					hidden : true,
				},{
					name : "sou",//"来源",
					index : "sou",
					width: 240,
					hidden : true,
				},{
					name : "agent",//"旅行社",
					index : "agent",
					width: 240,
					hidden : true,
				},{
					name : "flags",//"flags",
					index : "flags",
					width: 240,
					hidden : true,
				},{
					name : "comp_id",//"comp_id",
					index : "comp_id",
					width: 240,
					hidden : true,
				}],    	
//				scroll : true,
				scroll : 20,
				sortname : "pno",
				sortorder : "asc",
				viewrecords : true,
				rowNum : 20,
//				rowList:[10,20,30],
				emptyrecords: "没有搜索到任何数据",
				recordtext: "共计:{2}条",
				pager : pager_selector,
				altRows : true,  
				scrollrows:true,
				shrinkToFit : false,
		        ondblClickRow: function(rowid) {
		        	var rowdata = grid_selector.jqGrid("getRowData",rowid);
	        		el.find("#btnSave").trigger("click");
	        	}, 
	        	onSelectRow:function(rowid){
	        		var rowdata = grid_selector.jqGrid("getRowData",rowid);
	        		el.find("#profileInfo").find("span").each(function(){
			    		$(this).text(rowdata[this.id]);
			    	})
		        },
				loadComplete : function(data){
					var table = this;
					setTimeout(function(){
						//更新分页栏按钮
						updatePagerIcons(table);
					}, 0);
					//选中首行
//					var ids = grid_selector.jqGrid('getDataIDs');
//					grid_selector.jqGrid("setSelection",ids[0]);
//					if(data.rows.length==0)
						//返回查询的条件值
//						param.searchValueBack(param.search);
				}, 
//				scroll : function(){
//				}
			});
		},
		setGridValue : function(){//为jqgrid赋值
			var grid_selector = this.el.find("#gridTableFromProfile");
			grid_selector.jqGrid("GridUnload");
			this.initGrid();
		},
		getDs_1 : function(){//获取jqgrid数据源
			var el = this.el;
			var c = {"arg":{"like":el.find("#like").val(),"all_search":"","arg":"个人"},"where":{"gname":""}}
			c.where.gname = el.find("#gname").val();
			c.where.fname = el.find("#gname").val();
			c.where.tel = el.find("#tel").val();
			c.where.passno = el.find("#passno").val();
			return c;
		},
		getGridCon : function(){//获取过滤条件
			var search = this.param.search; 
			var where = {"ho_profile_where":{"search":search}};
			var wherestring = "";
			jQuery.ajax({  
				type : 'POST',
				async:false, 
				url : $.HSAPI_AJAXURL+$.HSAPI_PROCEDURE2_LIST,
				data : $.HSAPI_ASKJSONINFO( JSON.stringify(where),'HoWSASCSelectHoProfileAndTrace','{}'),
				dataType : 'json',  
				success : function(data){
		            if(data.RETURN == 1){
		            	wherestring = data.ho_profileSQL;
		            }else{
		            	jf_alert(data.MSG)
					}
				},  
				error : function(data){  
					systoast('ExecRetrieveJson'+$.i18n.propJsp("错误提示")+':',$.i18n.propJsp("系统后台提交错")+'!' + JSON.stringify(data),'error')
				}  
			});
			return wherestring;
		},
		saveProfile:function(num,profile,pnoname){//保存客人档案
			
			var param = this.param;
			var el = this.el;
			var str= 0;
			var t = this;
			var parameter = {};
			parameter.ho_profile = profile;
			parameter.ho_guest = {gno:param.gno};
			parameter.arg = {"pnoname":pnoname};
			//取数据并显示
			$.ajax({  
				type : 'POST',
				async:false, 
				url : $.HSAPI_AJAXURL+$.HSAPI_PROCEDURE2_LIST,
				data : $.HSAPI_ASKJSONINFO( JSON.stringify(parameter),'HoReservationProfileSaveExchange','{}'),
				dataType : 'json',  
				success : function(data){
		            if(data.RETURN == 1){
						str =1
		            }else if(data.RETURN==10){
		            	if(!$.isFunction($().used_profile_list)){
		    				$.ajaxSetup({ async: false, cache: false});
		    	            $.getScript("js/frontpos/jquery.modal.used_profile_list.js");
		    	            $.ajaxSetup({ async: true });
		    			}
		            	el.modal("toggle");
		            	$("#modal-cascade").remove();
						var div = '<div id="modal-cascade" class="modal"></div>';
						$("#pagemain script:first").before(div);
						$("#modal-cascade").modal("show");
						$("#modal-cascade").used_profile_list({
							ds : data.ho_guest,
							closeBack : function(){
								param.dataBack();
								t.activeProfile(profile.pno);//激活档案
								el.modal("hide");
								el.empty();
							}
						})
		            }else{
						jf_alert(data.MSG)
					}
				},  
				error : function(data){  
					systoast($.i18n.propJsp("修改陪同")+'HoReservationProfileSave'+$.i18n.propJsp("错误提示")+':',$.i18n.propJsp("系统后台提交错")+'!' + JSON.stringify(data),'error')
				}  
			});
			return str;
		},
		activeProfile:function(pno){//保存客人档案

			var parameter = {ho_profile:{pno:pno}};
			//取数据并显示
			$.ajax({  
				type : 'POST',
				async:false, 
				url : $.HSAPI_AJAXURL+$.HSAPI_PROCEDURE2_LIST, 
				data : $.HSAPI_ASKJSONINFO( JSON.stringify(parameter),'HoProfileHisActivate','{}'),
				dataType : 'json',  
				success : function(data){
				},  
				error : function(data){  
					systoast($.i18n.propJsp("激活失败")+'HoProfileHisActivate'+$.i18n.propJsp("错误提示")+':',$.i18n.propJsp("系统后台提交错")+'!' + JSON.stringify(data),'error')
				}  
			});
		},
	}
	
	/*
	 * 私有函数
	 * 翻页栏
	 * 2016-04-20
	 */
	function updatePagerIcons(table) {
		var replacement = {
				"ui-icon-seek-first" : "ace-icon fa fa-angle-double-left bigger-140",
				"ui-icon-seek-prev" : "ace-icon fa fa-angle-left bigger-140",
				"ui-icon-seek-next" : "ace-icon fa fa-angle-right bigger-140",
				"ui-icon-seek-end" : "ace-icon fa fa-angle-double-right bigger-140"
		};
		$(".ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon").each(function() {
			var icon = $(this);
			var $class = $.trim(icon.attr("class").replace("ui-icon", ""));

			if ($class in replacement)
				icon.attr("class", "ui-icon " + replacement[$class]);
		})
	};
})(jQuery);